package com.tian.service.impl.manage;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tian.enums.Sex;
import com.tian.mapper.UserManageMapperCustom;
import com.tian.pojo.Users;
import com.tian.pojo.bo.UserManageBO;
import com.tian.pojo.vo.ItemCommentVO;
import com.tian.pojo.vo.UserManageVO;
import com.tian.service.UserService;
import com.tian.service.manage.UserManageService;
import com.tian.utils.DateUtil;
import com.tian.utils.DesensitizationUtil;
import com.tian.utils.MD5Utils;
import com.tian.utils.PagedGridResult;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class UserManageServiceImpl implements UserManageService {

    @Autowired
    private Sid sid;
    @Autowired
    private UserManageMapperCustom userManageMapperCustom;
    @Autowired
    private UserService userService;

    private static final String USER_FACE="http://122.152.205.72:88/group1/M00/00/05/CpoxxFw_8_qAIlFXAAAcIhVPdSg994.png";

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PagedGridResult queryPagedUserManage(Integer page, Integer pageSize) {
        /**
         * page: 第几页
         * pageSize: 每页显示条数
         */
        PageHelper.startPage(page,pageSize);
        List<UserManageVO> list = userManageMapperCustom.queryUserManageInfo();
        for (UserManageVO userManageVO:list){
            Integer isAdmin = userManageVO.getIsAdmin();
            Integer sex = userManageVO.getSex();
            if (isAdmin==0){
                userManageVO.setIsAdminStr("普通用户");
            }
            if (isAdmin==1){
                userManageVO.setIsAdminStr("管理员");
            }
            if (sex==0){
                userManageVO.setSexStr("女");
            }
            if (sex==1){
                userManageVO.setSexStr("男");
            }
            if (sex==2){
                userManageVO.setSexStr("男");
            }


        }

        return setterPagedGrid(list,page);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<UserManageVO> getUserManageByUserName(String username) {
        UserManageVO userManageByUserName = userManageMapperCustom.getUserManageByUserName(username);
        List<UserManageVO> list=new ArrayList<>();
        list.add(userManageByUserName);
        return list;
    }

    @Override
    public int insertUserManage(UserManageBO userManageBO) {
        String userId = sid.nextShort();
        Users user=new Users();
        user.setId(userId);
        user.setUsername(userManageBO.getUsername());
        try {
            user.setPassword(MD5Utils.getMD5Str("123456"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        //默认用户昵称同用户名
        user.setNickname(userManageBO.getUsername());
        //默认头像
        user.setFace(USER_FACE);
        //默认生日
        user.setBirthday(DateUtil.stringToDate("1900-01-01"));

        if (userManageBO.getSex().equals("男")){
            user.setSex(1);
        }
        if (userManageBO.getSex().equals("女")){
            user.setSex(0);
        }

        user.setMobile(userManageBO.getMobile());
        user.setEmail(userManageBO.getEmail());
        user.setIs_admin(userManageBO.getIsAdmin());

        user.setCreatedTime(new Date());
        user.setUpdatedTime(new Date());

        int i = userManageMapperCustom.insertUserManage(user);
        return i;

    }

    @Override
    public int deleteUserManage(String username) {
        int i = userManageMapperCustom.deleteUserManage(username);
        return i;
    }

    @Override
    public int updateUserManage(UserManageBO userManageBO) {
        String id = userService.queryUserIdByUsername(userManageBO.getUsername());
        Integer sex=0;
        if (userManageBO.getSex().equals("男")){
           sex=1;
        }
        if (userManageBO.getSex().equals("女")){
            sex=0;
        }

        Map<String,Object> map =new HashMap<>();
        map.put("username",userManageBO.getUsername());
        map.put("mobile",userManageBO.getMobile());
        map.put("email",userManageBO.getEmail());
        map.put("sex",sex);
        map.put("isAdmin",userManageBO.getIsAdmin());
        map.put("id",id);
        return userManageMapperCustom.updateUserManage(map);
    }


    private PagedGridResult setterPagedGrid(List<?> list,Integer page){
        PageInfo<?> pageList =new PageInfo<>(list);
        PagedGridResult grid=new PagedGridResult();
        grid.setPage(page);
        grid.setRows(list);
        grid.setTotal(pageList.getPages());
        grid.setRecords(pageList.getTotal());
        return grid;
    }
}
